﻿/*!	@file	r_draw_core_dx11.h
	@brief	描画システム
Copyright (c) 2010 Yuya Yamaguchi

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include "r_draw_core_dx11.h"
#if R_TARGET_UNIQUEIS(R_TARGET_UNIQUE_WINDX11)
#include "../../r_debug.h"
#include "../r_draw_def.h"
#include "r_draw_swapchain_dx11.h"
#include "r_draw_sampler_dx11.h"
#include "r_draw_rasterizerState_dx11.h"
#include "r_draw_texture2d_dx11.h"
#include "r_draw_rendertarget_dx11.h"
#include "r_draw_depthstencil_dx11.h"
#include "r_draw_blendState_dx11.h"
#include "r_draw_depthstencilState_dx11.h"

#include "r_draw_inputlayout_dx11.h"
#include "r_draw_vertexbuffer_dx11.h"
#include "r_draw_indexbuffer_dx11.h"
#include "r_draw_constantbuffer_dx11.h"
#include "r_draw_vertexShader_dx11.h"
#include "r_draw_pixelShader_dx11.h"

#include "r_draw_context_dx11.h"

#include "../../window/r_window_systeminfo.h"
#include "../../util/r_util_memory_zero.h"
#include "../../util/r_util_com_release.h"

#include <d3dx11.h>

#ifdef _DEBUG
#pragma comment(lib, "d3d11.lib")
#pragma comment(lib, "d3dx11.lib")
#pragma comment(lib, "d3dcompiler.lib")
#else
#pragma comment(lib, "d3d11.lib")
#pragma comment(lib, "d3dx11.lib")
#pragma comment(lib, "d3dcompiler.lib")
#endif

// >>>>>>>>>> RTTI >>>>>>>>>>
#include "../../rtti/r_rtti_reflection_implement.h"
namespace r{
	namespace draw{
		namespace dx11{
RRTTI_REFLECTION_IMPLEMENT ( r::draw::dx11::CoreDX11, r::draw::Core )

// <<<<<<<<<< RTTI <<<<<<<<<<

			CoreDX11 :: CoreDX11()
				:
				device ( NULL ),
				context ( NULL ),
				swapChain ( NULL ),
				coreRenderTarget ( NULL ),
				coreDepthStencil ( NULL ),
				errCount ( 0 )
			{
				createDevice();
				createRenderTargetView();
				createDepthStencilView();
				createThreadContext();
			}

			CoreDX11 ::  ~CoreDX11()
			{
				delete coreRenderTarget;
				delete coreDepthStencil;
				delete reinterpret_cast<SwapChainDX11*> ( swapChain );
				r_tm_comRelease ( device );
				r_tm_comRelease ( context );
			}

			DXGI_FORMAT	CoreDX11 :: convertImageFormat ( r_ui32 format ) const
			{
				switch ( format ) {
				case ImageFormat_RGBA32_F:
					return DXGI_FORMAT_R32G32B32A32_FLOAT;
				case ImageFormat_RGBA32_U:
					return DXGI_FORMAT_R32G32B32A32_UINT;
				case ImageFormat_RGBA32_S:
					return DXGI_FORMAT_R32G32B32A32_SINT;
				case ImageFormat_RGB32_F:
					return DXGI_FORMAT_R32G32B32_FLOAT;
				case ImageFormat_RGB32_U:
					return DXGI_FORMAT_R32G32B32_UINT;
				case ImageFormat_RGB32_S:
					return DXGI_FORMAT_R32G32B32_SINT;
				case ImageFormat_RGBA16_F:
					return DXGI_FORMAT_R16G16B16A16_FLOAT;
				case ImageFormat_RGBA16_UN:
					return DXGI_FORMAT_R16G16B16A16_UNORM;
				case ImageFormat_RGBA16_U:
					return DXGI_FORMAT_R16G16B16A16_UINT;
				case ImageFormat_RGBA16_SN:
					return DXGI_FORMAT_R16G16B16A16_SNORM;
				case ImageFormat_RGBA16_S:
					return DXGI_FORMAT_R16G16B16A16_SINT;
				case ImageFormat_RG32_F:
					return DXGI_FORMAT_R32G32_FLOAT;
				case ImageFormat_RG32_U:
					return DXGI_FORMAT_R32G32_UINT;
				case ImageFormat_RG32_S:
					return DXGI_FORMAT_R32G32_SINT;
				case ImageFormat_D32_F_S32_U:
					return DXGI_FORMAT_D32_FLOAT_S8X24_UINT;
				case ImageFormat_RGB10A2_UN:
					return DXGI_FORMAT_R10G10B10A2_UNORM;
				case ImageFormat_RGB10A2_U:
					return DXGI_FORMAT_R10G10B10A2_UINT;
				case ImageFormat_RG11B10_F:
					return DXGI_FORMAT_R11G11B10_FLOAT;
				case ImageFormat_RGBA8_UN:
					return DXGI_FORMAT_R8G8B8A8_UNORM;
				case ImageFormat_RGBA8_U:
					return DXGI_FORMAT_R8G8B8A8_UINT;
				case ImageFormat_RGBA8_SN:
					return DXGI_FORMAT_R8G8B8A8_SNORM;
				case ImageFormat_RGBA8_S:
					return DXGI_FORMAT_R8G8B8A8_SINT;
				case ImageFormat_RG16_F:
					return DXGI_FORMAT_R16G16_FLOAT;
				case ImageFormat_RG16_UN:
					return DXGI_FORMAT_R16G16_UNORM;
				case ImageFormat_RG16_U:
					return DXGI_FORMAT_R16G16_UINT;
				case ImageFormat_RG16_SN:
					return DXGI_FORMAT_R16G16_SNORM;
				case ImageFormat_RG16_S:
					return DXGI_FORMAT_R16G16_SINT;
				case ImageFormat_D32_F:
					return DXGI_FORMAT_D32_FLOAT;
				case ImageFormat_R32_F:
					return DXGI_FORMAT_R32_FLOAT;
				case ImageFormat_R32_U:
					return DXGI_FORMAT_R32_UINT;
				case ImageFormat_R32_S:
					return DXGI_FORMAT_R32_SINT;
				case ImageFormat_D24_UN_S8_U:
					return DXGI_FORMAT_D24_UNORM_S8_UINT;
				case ImageFormat_RG8_UN:
					return DXGI_FORMAT_R8G8_UNORM;
				case ImageFormat_RG8_U:
					return DXGI_FORMAT_R8G8_UINT;
				case ImageFormat_RG8_SN:
					return DXGI_FORMAT_R8G8_SNORM;
				case ImageFormat_RG8_S:
					return DXGI_FORMAT_R8G8_SINT;
				case ImageFormat_R16_F:
					return DXGI_FORMAT_R16_FLOAT;
				case ImageFormat_D16_UN:
					return DXGI_FORMAT_D16_UNORM;
				case ImageFormat_R16_UN:
					return DXGI_FORMAT_R16_UNORM;
				case ImageFormat_R16_U:
					return DXGI_FORMAT_R16_UINT;
				case ImageFormat_R16_SN:
					return DXGI_FORMAT_R16_SNORM;
				case ImageFormat_R16_S:
					return DXGI_FORMAT_R16_SINT;
				case ImageFormat_R8_UN:
					return DXGI_FORMAT_R8_UNORM;
				case ImageFormat_R8_U:
					return DXGI_FORMAT_R8_UINT;
				case ImageFormat_R8_SN:
					return DXGI_FORMAT_R8_SNORM;
				case ImageFormat_R8_S:
					return DXGI_FORMAT_R8_SINT;
				case ImageFormat_A8_UN:
					return DXGI_FORMAT_A8_UNORM;
				case ImageFormat_BC1_UN:
					return DXGI_FORMAT_BC1_UNORM;
				case ImageFormat_BC2_UN:
					return DXGI_FORMAT_BC2_UNORM;
				case ImageFormat_BC3_UN:
					return DXGI_FORMAT_BC3_UNORM;
				case ImageFormat_BC4_UN:
					return DXGI_FORMAT_BC4_UNORM;
				case ImageFormat_BC4_SN:
					return DXGI_FORMAT_BC4_SNORM;
				case ImageFormat_BC5_UN:
					return DXGI_FORMAT_BC5_UNORM;
				case ImageFormat_BC5_SN:
					return DXGI_FORMAT_BC5_SNORM;
				case ImageFormat_BC6_UF:
					return DXGI_FORMAT_BC6H_UF16;
				case ImageFormat_BC6_SF:
					return DXGI_FORMAT_BC6H_SF16;
				case ImageFormat_BC7_UN:
					return DXGI_FORMAT_BC7_UNORM;
				}
				return DXGI_FORMAT_UNKNOWN;
			}

			D3D11_USAGE	CoreDX11 :: convertImageUsage ( r_ui32 usage ) const
			{
				switch ( usage ) {
				case ImageUsageType_default:
					return D3D11_USAGE_DEFAULT;
				case ImageUsageType_dynamic:
					return D3D11_USAGE_DYNAMIC;
				}
				return D3D11_USAGE_DEFAULT;
			}

			r_ui32	CoreDX11 :: convertImageBind ( r_ui32 bind ) const
			{
				switch ( bind ) {
				case ImageBindFlag_RenderTarget:
					return D3D11_BIND_RENDER_TARGET;
				case ImageBindFlag_DepthStencil:
					return D3D11_BIND_DEPTH_STENCIL;
				case ImageBindFlag_StreamOut:
					return D3D11_BIND_STREAM_OUTPUT;
				case ImageBindFlag_ShaderResource:
					return D3D11_BIND_SHADER_RESOURCE;
				}
				return 0;
			}

			r_ui32	CoreDX11 :: convertImageBindFlag ( r_ui32 bindflag ) const
			{
				const r_ui32 bindcount = 4;
				r_ui32	newflag = 0;
				for ( r_ui32 i = 0; i < bindcount; ++i ) {
					if ( bindflag & ( 1 << i ) ) {
						newflag |= convertImageBind ( 1 << i );
					}
				}
				return newflag;
			}

			D3D11_PRIMITIVE_TOPOLOGY	CoreDX11 :: convertPrimitiveToporogy ( r_ui32 toporogy ) const
			{
				switch ( toporogy ) {
				case PrimitiveTopologyType_PointList:
					return D3D11_PRIMITIVE_TOPOLOGY_POINTLIST;
				case PrimitiveTopologyType_LineList:
					return D3D11_PRIMITIVE_TOPOLOGY_LINELIST;
				case PrimitiveTopologyType_LineStrip:
					return D3D11_PRIMITIVE_TOPOLOGY_LINESTRIP;
				case PrimitiveTopologyType_TriangleList:
					return D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST;
				case PrimitiveTopologyType_TriangleStrip:
					return D3D11_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP;
				case PrimitiveTopologyType_LineListADJ:
					return D3D11_PRIMITIVE_TOPOLOGY_LINELIST_ADJ;
				case PrimitiveTopologyType_LineStripADJ:
					return D3D11_PRIMITIVE_TOPOLOGY_LINESTRIP_ADJ;
				case PrimitiveTopologyType_TriangleListADJ:
					return D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST_ADJ;
				case PrimitiveTopologyType_TriangleStripADJ:
					return D3D11_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP_ADJ;
				}
				return D3D11_PRIMITIVE_TOPOLOGY_UNDEFINED;
			}

			DXGI_FORMAT	CoreDX11 :: convertInputLayoutFormat ( r_ui32 format ) const
			{
				switch ( format ) {
				case InputLayoutFormat_Float:
					return DXGI_FORMAT_R32_FLOAT;
				case InputLayoutFormat_FVec2:
					return DXGI_FORMAT_R32G32_FLOAT;
				case InputLayoutFormat_FVec3:
					return DXGI_FORMAT_R32G32B32_FLOAT;
				case InputLayoutFormat_FVec4:
					return DXGI_FORMAT_R32G32B32A32_FLOAT;
				case InputLayoutFormat_UByte4:
					return DXGI_FORMAT_R8G8B8A8_UINT;
				case InputLayoutFormat_UNorm4:
					return DXGI_FORMAT_R8G8B8A8_UNORM;
				case InputLayoutFormat_SNorm4:
					return DXGI_FORMAT_R8G8B8A8_SNORM;
				}
				return DXGI_FORMAT_UNKNOWN;
			}

			D3D11_FILTER	CoreDX11 :: convertFilter ( r_ui32 filter ) const
			{
				switch ( filter ) {
				case SamplerFilterType_MIN_MAG_MIP_POINT:
					return D3D11_FILTER_MIN_MAG_MIP_POINT;
				case SamplerFilterType_MIN_MAG_POINT_MIP_LINEAR:
					return D3D11_FILTER_MIN_MAG_POINT_MIP_LINEAR;
				case SamplerFilterType_MIN_POINT_MAG_LINEAR_MIP_POINT:
					return D3D11_FILTER_MIN_POINT_MAG_LINEAR_MIP_POINT;
				case SamplerFilterType_MIN_POINT_MAG_MIP_LINEAR:
					return D3D11_FILTER_MIN_POINT_MAG_MIP_LINEAR;
				case SamplerFilterType_MIN_LINEAR_MAG_MIP_POINT:
					return D3D11_FILTER_MIN_LINEAR_MAG_MIP_POINT;
				case SamplerFilterType_MIN_LINEAR_MAG_POINT_MIP_LINEAR:
					return D3D11_FILTER_MIN_LINEAR_MAG_POINT_MIP_LINEAR;
				case SamplerFilterType_MIN_MAG_LINEAR_MIP_POINT:
					return D3D11_FILTER_MIN_MAG_LINEAR_MIP_POINT;
				case SamplerFilterType_MIN_MAG_MIP_LINEAR:
					return D3D11_FILTER_MIN_MAG_MIP_LINEAR;
				case SamplerFilterType_ANISOTROPIC:
					return D3D11_FILTER_ANISOTROPIC;
				case SamplerFilterType_COMPARISON_MIN_MAG_MIP_POINT:
					return D3D11_FILTER_COMPARISON_MIN_MAG_MIP_POINT;
				case SamplerFilterType_COMPARISON_MIN_MAG_POINT_MIP_LINEAR:
					return D3D11_FILTER_COMPARISON_MIN_MAG_POINT_MIP_LINEAR;
				case SamplerFilterType_COMPARISON_MIN_POINT_MAG_LINEAR_MIP_POINT:
					return D3D11_FILTER_COMPARISON_MIN_POINT_MAG_LINEAR_MIP_POINT;
				case SamplerFilterType_COMPARISON_MIN_POINT_MAG_MIP_LINEAR:
					return D3D11_FILTER_COMPARISON_MIN_POINT_MAG_MIP_LINEAR;
				case SamplerFilterType_COMPARISON_MIN_LINEAR_MAG_MIP_POINT:
					return D3D11_FILTER_COMPARISON_MIN_LINEAR_MAG_MIP_POINT;
				case SamplerFilterType_COMPARISON_MIN_LINEAR_MAG_POINT_MIP_LINEAR:
					return D3D11_FILTER_COMPARISON_MIN_LINEAR_MAG_POINT_MIP_LINEAR;
				case SamplerFilterType_COMPARISON_MIN_MAG_LINEAR_MIP_POINT:
					return D3D11_FILTER_COMPARISON_MIN_MAG_LINEAR_MIP_POINT;
				case SamplerFilterType_COMPARISON_MIN_MAG_MIP_LINEAR:
					return D3D11_FILTER_COMPARISON_MIN_MAG_MIP_LINEAR;
				case SamplerFilterType_COMPARISON_ANISOTROPIC:
					return D3D11_FILTER_COMPARISON_ANISOTROPIC;
				};
				return D3D11_FILTER_MIN_MAG_MIP_POINT;
			}

			D3D11_TEXTURE_ADDRESS_MODE	CoreDX11 :: convertAddressMode ( r_ui32 address ) const
			{
				switch ( address ) {
				case SamplerAddressMode_WRAP:
					return D3D11_TEXTURE_ADDRESS_WRAP;
				case SamplerAddressMode_MIRROR:
					return D3D11_TEXTURE_ADDRESS_MIRROR;
				case SamplerAddressMode_CLAMP:
					return D3D11_TEXTURE_ADDRESS_CLAMP;
				case SamplerAddressMode_BORDER:
					return D3D11_TEXTURE_ADDRESS_BORDER;
				case SamplerAddressMode_MIRROR_ONCE:
					return D3D11_TEXTURE_ADDRESS_MIRROR_ONCE;
				}
				return D3D11_TEXTURE_ADDRESS_WRAP;
			}

			D3D11_COMPARISON_FUNC	CoreDX11 :: convertComparisonFunc ( r_ui32 compfunc ) const
			{
				switch ( compfunc ) {
				case SamplerComparisonFunc_NEVER:
					return D3D11_COMPARISON_NEVER;
				case SamplerComparisonFunc_LESS:
					return D3D11_COMPARISON_LESS;
				case SamplerComparisonFunc_EQUAL:
					return D3D11_COMPARISON_EQUAL;
				case SamplerComparisonFunc_LESS_EQUAL:
					return D3D11_COMPARISON_LESS_EQUAL;
				case SamplerComparisonFunc_GREATER:
					return D3D11_COMPARISON_GREATER;
				case SamplerComparisonFunc_NOT_EQUAL:
					return D3D11_COMPARISON_NOT_EQUAL;
				case SamplerComparisonFunc_GREATER_EQUAL:
					return D3D11_COMPARISON_GREATER_EQUAL;
				case SamplerComparisonFunc_ALWAYS:
					return D3D11_COMPARISON_ALWAYS;
				}
				return D3D11_COMPARISON_NEVER;
			}

			D3D11_FILL_MODE CoreDX11 :: convertFillMode ( r_ui32 fill ) const
			{
				switch ( fill ) {
				case FillMode_Wire:
					return D3D11_FILL_WIREFRAME;
				case FillMode_Solid:
					return D3D11_FILL_SOLID;
				}
				return D3D11_FILL_SOLID;
			}

			D3D11_CULL_MODE CoreDX11 :: convertCullMode ( r_ui32 cull ) const
			{
				switch ( cull ) {
				case CullMode_None:
					return D3D11_CULL_NONE;
				case CullMode_Front:
					return D3D11_CULL_FRONT;
				case CullMode_Back:
					return D3D11_CULL_BACK;
				}
				return D3D11_CULL_NONE;
			}

			D3D11_BLEND CoreDX11 :: convertBlend ( r_ui32 blend ) const
			{
				switch ( blend ) {
				case BlendType_ZERO:
					return D3D11_BLEND_ZERO;
				case BlendType_ONE:
					return D3D11_BLEND_ONE;
				case BlendType_SRC_COLOR:
					return D3D11_BLEND_SRC_COLOR;
				case BlendType_INV_SRC_COLOR:
					return D3D11_BLEND_INV_SRC_COLOR;
				case BlendType_SRC_ALPHA:
					return D3D11_BLEND_SRC_ALPHA;
				case BlendType_INV_SRC_ALPHA:
					return D3D11_BLEND_INV_SRC_ALPHA;
				case BlendType_DEST_ALPHA:
					return D3D11_BLEND_DEST_ALPHA;
				case BlendType_INV_DEST_ALPHA:
					return D3D11_BLEND_INV_DEST_ALPHA;
				case BlendType_DEST_COLOR:
					return D3D11_BLEND_DEST_COLOR;
				case BlendType_INV_DEST_COLOR:
					return D3D11_BLEND_INV_DEST_COLOR;
				case BlendType_SRC_ALPHA_SAT:
					return D3D11_BLEND_SRC_ALPHA_SAT;
				case BlendType_BLEND_FACTOR:
					return D3D11_BLEND_BLEND_FACTOR;
				case BlendType_INV_BLEND_FACTOR:
					return D3D11_BLEND_INV_BLEND_FACTOR;
				case BlendType_SRC1_COLOR:
					return D3D11_BLEND_SRC1_COLOR;
				case BlendType_INV_SRC1_COLOR:
					return D3D11_BLEND_INV_SRC1_COLOR;
				case BlendType_SRC1_ALPHA:
					return D3D11_BLEND_SRC1_ALPHA;
				case BlendType_INV_SRC1_ALPHA:
					return D3D11_BLEND_INV_SRC1_ALPHA;
				}
				return D3D11_BLEND_ZERO;
			}

			D3D11_BLEND_OP CoreDX11 :: convertBlendOP ( r_ui32 blendOP ) const
			{
				switch ( blendOP ) {
				case BlendOp_ADD:
					return D3D11_BLEND_OP_ADD;
				case BlendOp_SUBTRACT:
					return D3D11_BLEND_OP_SUBTRACT;
				case BlendOp_REV_SUBTRACT:
					return D3D11_BLEND_OP_REV_SUBTRACT;
				case BlendOp_MIN:
					return D3D11_BLEND_OP_MIN;
				case BlendOp_MAX:
					return D3D11_BLEND_OP_MAX;
				}
				return D3D11_BLEND_OP_ADD;
			}

			r_ui8 CoreDX11 :: convertBlendMask ( r_ui8 mask ) const
			{
				// 現状はDirectXとコンパチです
				return mask;
			}

			D3D11_COMPARISON_FUNC CoreDX11 :: convertDepthComparison ( r_ui32 depthcomp ) const
			{
				switch ( depthcomp ) {
				case DepthComparisonType_NEVER:
					return D3D11_COMPARISON_NEVER;
				case DepthComparisonType_LESS:
					return D3D11_COMPARISON_LESS;
				case DepthComparisonType_EQUAL:
					return D3D11_COMPARISON_EQUAL;
				case DepthComparisonType_LESS_EQUAL:
					return D3D11_COMPARISON_LESS_EQUAL;
				case DepthComparisonType_GREATER:
					return D3D11_COMPARISON_GREATER;
				case DepthComparisonType_NOT_EQUAL:
					return D3D11_COMPARISON_NOT_EQUAL;
				case DepthComparisonType_GREATER_EQUAL:
					return D3D11_COMPARISON_GREATER_EQUAL;
				case DepthComparisonType_ALWAYS:
					return D3D11_COMPARISON_ALWAYS;
				};
				return D3D11_COMPARISON_LESS;
			}

			D3D11_STENCIL_OP CoreDX11 :: convertStencilOp ( r_ui32 stencilop ) const
			{
				switch ( stencilop ) {
				case StencilOp_KEEP:
					return D3D11_STENCIL_OP_KEEP;
				case StencilOp_ZERO:
					return D3D11_STENCIL_OP_ZERO;
				case StencilOp_REPLACE:
					return D3D11_STENCIL_OP_REPLACE;
				case StencilOp_INCR_SAT:
					return D3D11_STENCIL_OP_INCR_SAT;
				case StencilOp_DECR_SAT:
					return D3D11_STENCIL_OP_DECR_SAT;
				case StencilOp_INVERT:
					return D3D11_STENCIL_OP_INVERT;
				case StencilOp_INCR:
					return D3D11_STENCIL_OP_INCR;
				case StencilOp_DECR:
					return D3D11_STENCIL_OP_DECR;
				}
				return D3D11_STENCIL_OP_KEEP;
			}

			D3D11_DEPTH_WRITE_MASK CoreDX11 :: convertDepthWriteMask ( r_ui32 mask ) const
			{
				switch ( mask ) {
				case DepthWriteMaskFlag_ZERO:
					return D3D11_DEPTH_WRITE_MASK_ZERO;
				case DepthWriteMaskFlag_ALL:
					return D3D11_DEPTH_WRITE_MASK_ALL;
				}
				return D3D11_DEPTH_WRITE_MASK_ALL;
			}


			ImageFormat CoreDX11 :: invertImageFormat ( r_ui32 format ) const
			{
				switch ( format ) {
				case DXGI_FORMAT_R32G32B32A32_FLOAT:
					return ImageFormat_RGBA32_F;
				case DXGI_FORMAT_R32G32B32A32_UINT:
					return ImageFormat_RGBA32_U;
				case DXGI_FORMAT_R32G32B32A32_SINT:
					return ImageFormat_RGBA32_S;
				case DXGI_FORMAT_R32G32B32_FLOAT:
					return ImageFormat_RGB32_F;
				case DXGI_FORMAT_R32G32B32_UINT:
					return ImageFormat_RGB32_U;
				case DXGI_FORMAT_R32G32B32_SINT:
					return ImageFormat_RGB32_S;
				case DXGI_FORMAT_R16G16B16A16_FLOAT:
					return ImageFormat_RGBA16_F;
				case DXGI_FORMAT_R16G16B16A16_UNORM:
					return ImageFormat_RGBA16_UN;
				case DXGI_FORMAT_R16G16B16A16_UINT:
					return ImageFormat_RGBA16_U;
				case DXGI_FORMAT_R16G16B16A16_SNORM:
					return ImageFormat_RGBA16_SN;
				case DXGI_FORMAT_R16G16B16A16_SINT:
					return ImageFormat_RGBA16_S;
				case DXGI_FORMAT_R32G32_FLOAT:
					return ImageFormat_RG32_F;
				case DXGI_FORMAT_R32G32_UINT:
					return ImageFormat_RG32_U;
				case DXGI_FORMAT_R32G32_SINT:
					return ImageFormat_RG32_S;
				case DXGI_FORMAT_D32_FLOAT_S8X24_UINT:
					return ImageFormat_D32_F_S32_U;
				case DXGI_FORMAT_R10G10B10A2_UNORM:
					return ImageFormat_RGB10A2_UN;
				case DXGI_FORMAT_R10G10B10A2_UINT:
					return ImageFormat_RGB10A2_U;
				case DXGI_FORMAT_R11G11B10_FLOAT:
					return ImageFormat_RG11B10_F;
				case DXGI_FORMAT_R8G8B8A8_UNORM:
					return ImageFormat_RGBA8_UN;
				case DXGI_FORMAT_R8G8B8A8_UINT:
					return ImageFormat_RGBA8_U;
				case DXGI_FORMAT_R8G8B8A8_SNORM:
					return ImageFormat_RGBA8_SN;
				case DXGI_FORMAT_R8G8B8A8_SINT:
					return ImageFormat_RGBA8_S;
				case DXGI_FORMAT_R16G16_FLOAT:
					return ImageFormat_RG16_F;
				case DXGI_FORMAT_R16G16_UNORM:
					return ImageFormat_RG16_UN;
				case DXGI_FORMAT_R16G16_UINT:
					return ImageFormat_RG16_U;
				case DXGI_FORMAT_R16G16_SNORM:
					return ImageFormat_RG16_SN;
				case DXGI_FORMAT_R16G16_SINT:
					return ImageFormat_RG16_S;
				case DXGI_FORMAT_D32_FLOAT:
					return ImageFormat_D32_F;
				case DXGI_FORMAT_R32_FLOAT:
					return ImageFormat_R32_F;
				case DXGI_FORMAT_R32_UINT:
					return ImageFormat_R32_U;
				case DXGI_FORMAT_R32_SINT:
					return ImageFormat_R32_S;
				case DXGI_FORMAT_D24_UNORM_S8_UINT:
					return ImageFormat_D24_UN_S8_U;
				case DXGI_FORMAT_R8G8_UNORM:
					return ImageFormat_RG8_UN;
				case DXGI_FORMAT_R8G8_UINT:
					return ImageFormat_RG8_U;
				case DXGI_FORMAT_R8G8_SNORM:
					return ImageFormat_RG8_SN;
				case DXGI_FORMAT_R8G8_SINT:
					return ImageFormat_RG8_S;
				case DXGI_FORMAT_R16_FLOAT:
					return ImageFormat_R16_F;
				case DXGI_FORMAT_D16_UNORM:
					return ImageFormat_D16_UN;
				case DXGI_FORMAT_R16_UNORM:
					return ImageFormat_R16_UN;
				case DXGI_FORMAT_R16_UINT:
					return ImageFormat_R16_U;
				case DXGI_FORMAT_R16_SNORM:
					return ImageFormat_R16_SN;
				case DXGI_FORMAT_R16_SINT:
					return ImageFormat_R16_S;
				case DXGI_FORMAT_R8_UNORM:
					return ImageFormat_R8_UN;
				case DXGI_FORMAT_R8_UINT:
					return ImageFormat_R8_U;
				case DXGI_FORMAT_R8_SNORM:
					return ImageFormat_R8_SN;
				case DXGI_FORMAT_R8_SINT:
					return ImageFormat_R8_S;
				case DXGI_FORMAT_A8_UNORM:
					return ImageFormat_A8_UN;
				case DXGI_FORMAT_BC1_UNORM:
					return ImageFormat_BC1_UN;
				case DXGI_FORMAT_BC2_UNORM:
					return ImageFormat_BC2_UN;
				case DXGI_FORMAT_BC3_UNORM:
					return ImageFormat_BC3_UN;
				case DXGI_FORMAT_BC4_UNORM:
					return ImageFormat_BC4_UN;
				case DXGI_FORMAT_BC4_SNORM:
					return ImageFormat_BC4_SN;
				case DXGI_FORMAT_BC5_UNORM:
					return ImageFormat_BC5_UN;
				case DXGI_FORMAT_BC5_SNORM:
					return ImageFormat_BC5_SN;
				case DXGI_FORMAT_BC6H_UF16:
					return ImageFormat_BC6_UF;
				case DXGI_FORMAT_BC6H_SF16:
					return ImageFormat_BC6_SF;
				case DXGI_FORMAT_BC7_UNORM:
					return ImageFormat_BC7_UN;
				}
				return ImageFormat_Unknown;
			}

			r_ui32 CoreDX11 :: invertImageUsage ( r_ui32 usage ) const
			{
				switch ( usage ) {
				case D3D11_USAGE_DEFAULT:
					return ImageUsageType_default;
				case D3D11_USAGE_DYNAMIC:
					return ImageUsageType_dynamic;
				}
				return ImageUsageType_default;
			}

			r_ui32 CoreDX11 :: invertImageBind ( r_ui32 bind ) const
			{
				switch ( bind ) {
				case D3D11_BIND_RENDER_TARGET:
					return ImageBindFlag_RenderTarget;
				case D3D11_BIND_DEPTH_STENCIL:
					return ImageBindFlag_DepthStencil;
				case D3D11_BIND_STREAM_OUTPUT:
					return ImageBindFlag_StreamOut;
				case D3D11_BIND_SHADER_RESOURCE:
					return ImageBindFlag_ShaderResource;
				}
				return 0;
			}

			r_ui32 CoreDX11 :: invertImageBindFlag ( r_ui32 bindflag ) const
			{
				const r_ui32 bindcount = 9;
				r_ui32	newflag = 0;
				for ( r_ui32 i = 0; i < bindcount; ++i ) {
					if ( bindflag & ( 1 << i ) ) {
						newflag |= invertImageBind ( 1 << i );
					}
				}
				return newflag;
			}

			ID3D11Device* CoreDX11 :: getD3DDevice() const
			{
				return device;
			}

			ID3D11DeviceContext* CoreDX11 :: getD3DContext() const
			{
				return context;
			}

			D3D_FEATURE_LEVEL	CoreDX11 :: getFeatureLevel() const
			{
				return device->GetFeatureLevel();
			}

			void CoreDX11 :: createDevice()
			{
				// この辺は初期化関係を載せてくれているサイトさんのコピペです

				// デバイス初期化情報
				IDXGIAdapter*	adapter = NULL;
				D3D_DRIVER_TYPE	dtype = D3D_DRIVER_TYPE_HARDWARE;
				r_ui32			flags = 0;
				D3D_FEATURE_LEVEL	featureLevels[] = {
					D3D_FEATURE_LEVEL_11_0,
					D3D_FEATURE_LEVEL_10_1,
					D3D_FEATURE_LEVEL_10_0,
					D3D_FEATURE_LEVEL_9_3,
					D3D_FEATURE_LEVEL_9_2,
					D3D_FEATURE_LEVEL_9_1
				};
				r_ui32				numFeatureLevels = sizeof ( featureLevels ) / sizeof ( D3D_FEATURE_LEVEL );
				r_ui32				sdkVersion = D3D11_SDK_VERSION;
				D3D_FEATURE_LEVEL	validFeatureLevel;

				// システム情報の取得
				cRWindowSystemInfo	sysinfo;
				r_si32				winWidth;
				r_si32				winHeight;
				sysinfo.getWindwowSize ( winWidth, winHeight );

				// スワップチェインの設定
				DXGI_SWAP_CHAIN_DESC	scDesc;
				r_tm_memoryZero ( scDesc );
				scDesc.BufferCount			= 1;
				scDesc.BufferDesc.Width		= winWidth;
				scDesc.BufferDesc.Height	= winHeight;
				scDesc.BufferDesc.Format	= DXGI_FORMAT_R8G8B8A8_UNORM;
				scDesc.BufferDesc.RefreshRate.Numerator	= 60;
				scDesc.BufferDesc.RefreshRate.Denominator	= 1;
				scDesc.BufferUsage			= DXGI_USAGE_RENDER_TARGET_OUTPUT;
				scDesc.OutputWindow			= reinterpret_cast<HWND> ( sysinfo.getWindowhandle() );
				scDesc.SampleDesc.Count		= 1;
				scDesc.SampleDesc.Quality	= 0;
				scDesc.Windowed				= TRUE;

				// デバイスとスワップチェインを作成する
				IDXGISwapChain*				chain;
				HRESULT hr = D3D11CreateDeviceAndSwapChain (
								 adapter,
								 dtype,
								 NULL,
								 flags,
								 featureLevels,
								 numFeatureLevels,
								 sdkVersion,
								 &scDesc,
								 &chain,
								 &device,
								 &validFeatureLevel,
								 &context );
				swapChain = new SwapChainDX11 ( chain );
				if ( FAILED ( hr ) ) {
					++errCount;
					RDEBUG_WARRN ( _RST ( "failed create device %08x\n" ), hr );
				}
			}

			void	CoreDX11 :: createRenderTargetView()
			{
				// レンダーターゲット作成コード
				ID3D11Texture2D*	backBuffer = NULL;
				HRESULT				hr = swapChain->swapChain->GetBuffer ( 0, __uuidof ( ID3D11Texture2D ), reinterpret_cast<void**> ( &backBuffer ) );
				if ( FAILED ( hr ) ) {
					++errCount;
					RDEBUG_WARRN ( _RST ( "failed create backbuffer %08x\n" ), hr );
					return;
				}
				ID3D11RenderTargetView*	renderTarget = NULL;
				hr = device->CreateRenderTargetView ( backBuffer, NULL, &renderTarget );
				if ( FAILED ( hr ) ) {
					++errCount;
					RDEBUG_WARRN ( _RST ( "failed create rendertarget %08x\n" ), hr );
					return;
				}
				D3D11_TEXTURE2D_DESC	dxdesc;
				backBuffer->GetDesc ( &dxdesc );
				Texture2DDesc	texdesc;
				texdesc.width			= dxdesc.Width;
				texdesc.height			= dxdesc.Height;
				texdesc.mipLevels		= dxdesc.MipLevels;
				texdesc.format			= invertImageFormat ( dxdesc.Format );
				texdesc.sampleCount		= dxdesc.SampleDesc.Count;
				texdesc.sampleQuality	= dxdesc.SampleDesc.Quality;
				texdesc.usage			= invertImageUsage ( dxdesc.Usage );
				texdesc.bindFlags		= invertImageBindFlag ( dxdesc.BindFlags );
				coreRenderTarget = new RenderTargetDX11 ( renderTarget, new Texture2DDX11 ( backBuffer, texdesc, NULL ) );
			}

			void	CoreDX11 :: createDepthStencilView()
			{
				// システム情報の取得
				cRWindowSystemInfo	sysinfo;
				r_si32				winWidth;
				r_si32				winHeight;
				sysinfo.getWindwowSize ( winWidth, winHeight );

				coreDepthStencil = reinterpret_cast<DepthStencilDX11*> ( createDepthStencil ( winWidth, winHeight, ImageFormat_D24_UN_S8_U, _RST ( "core depthstencil" ) ) );
			}

			void	CoreDX11 :: createThreadContext()
			{
				threadContext = new ContextDX11();
			}

			const SwapChain*	CoreDX11 :: getSwapChain() const
			{
				return swapChain;
			}

			r::draw::Texture2D*	CoreDX11 :: createTexture2D ( const Texture2DDesc& desc )
			{
				D3D11_TEXTURE2D_DESC	dxdesc;
				dxdesc.Width			= desc.width;
				dxdesc.Height			= desc.height;
				dxdesc.Format			= convertImageFormat ( desc.format );
				dxdesc.MipLevels		= desc.mipLevels;
				dxdesc.SampleDesc.Count		= desc.sampleCount;
				dxdesc.SampleDesc.Quality	= desc.sampleQuality;
				dxdesc.Usage			= convertImageUsage ( desc.usage );
				dxdesc.BindFlags		= convertImageBindFlag ( desc.bindFlags );
				dxdesc.ArraySize		= 1;
				dxdesc.CPUAccessFlags	= desc.usage == ImageUsageType_dynamic ? ( D3D11_CPU_ACCESS_WRITE/* | D3D11_CPU_ACCESS_READ*/ ) : 0;
				dxdesc.MiscFlags		= 0;
				ID3D11Texture2D*		tex = NULL;
				HRESULT	res = device->CreateTexture2D ( &dxdesc, NULL, NULL );
				HRESULT	hr = device->CreateTexture2D ( &dxdesc, NULL, &tex );
				if ( FAILED ( hr ) ) {
					ErrUp();
					RDEBUG_WARRN ( _RST ( "failed create texture %08x\n" ), hr );
				}
				return new Texture2DDX11 ( tex, desc, createShaderResourceView ( tex ) );
			}

			r::draw::Texture2D*	CoreDX11 :: createTexture2D ( const Texture2DDesc& desc, const void* buff, r_size buffSize )
			{
				ID3D11Resource*			tex = NULL;
				D3DX11_IMAGE_LOAD_INFO	info;
				info.Width				= D3DX11_DEFAULT;
				info.Height				= D3DX11_DEFAULT;
				info.Depth				= D3DX11_DEFAULT;
				info.FirstMipLevel		= D3DX11_DEFAULT;
				info.MipLevels			= D3DX11_DEFAULT;
				info.Usage				= convertImageUsage ( desc.usage );
				info.BindFlags			= convertImageBindFlag ( desc.bindFlags );
				info.CpuAccessFlags		= D3DX11_DEFAULT;
				info.MiscFlags			= D3DX11_DEFAULT;
				info.Format				= DXGI_FORMAT_FROM_FILE;
				info.Filter				= D3DX11_DEFAULT;
				info.MipFilter			= D3DX11_DEFAULT;
				info.pSrcInfo			= NULL;

				HRESULT	hr = D3DX11CreateTextureFromMemory ( device, buff, buffSize, &info, NULL, &tex, NULL );
				if ( FAILED ( hr ) ) {
					ErrUp();
					RDEBUG_WARRN ( _RST ( "failed create texture %08x\n" ), hr );
				}
				return  new Texture2DDX11 ( reinterpret_cast<ID3D11Texture2D*> ( tex ), desc, createShaderResourceView ( reinterpret_cast<ID3D11Texture2D*> ( tex ) ) );
			}

			r::draw::RenderTarget*	CoreDX11 :: createRenderTarget ( r_ui32 width, r_ui32 height, ImageFormat format, const r_cstr name )
			{
				Texture2DDesc			texDesc;
				texDesc.width					= width;
				texDesc.height					= height;
				texDesc.mipLevels				= 1;
				texDesc.format					= format;
				texDesc.sampleCount				= 1;
				texDesc.sampleQuality			= 0;
				texDesc.usage					= ImageUsageType_default;
				texDesc.bindFlags				= ImageBindFlag_RenderTarget | ImageBindFlag_ShaderResource;
				Texture2DDX11* targettex	= r::rtti::dynamicCast<Texture2DDX11*> ( createTexture2D ( texDesc ) );

				ID3D11RenderTargetView*	renderTarget	= NULL;
				HRESULT	hr	= device->CreateRenderTargetView ( targettex->texture, NULL, &renderTarget );
				if ( FAILED ( hr ) ) {
					ErrUp();
					RDEBUG_WARRN ( _RST ( "failed create renderTarget %08x\n" ), hr );
					delete	targettex;
					return	NULL;
				}
				return new RenderTargetDX11 ( renderTarget, targettex );
			}

			r::draw::DepthStencil*	CoreDX11 :: createDepthStencil ( r_ui32 width, r_ui32 height, ImageFormat format, const r_cstr name )
			{
				Texture2DDesc			depthDesc;
				depthDesc.width					= width;
				depthDesc.height				= height;
				depthDesc.mipLevels				= 1;
				depthDesc.format				= format;
				depthDesc.sampleCount			= 1;
				depthDesc.sampleQuality			= 0;
				depthDesc.usage					= ImageUsageType_default;
				depthDesc.bindFlags				= ImageBindFlag_DepthStencil;
				Texture2DDX11* depthtex	= reinterpret_cast<Texture2DDX11*> ( createTexture2D ( depthDesc ) );
				ID3D11DepthStencilView*	depthStencil	= NULL;
				// 深度バッファターゲットを作成する
				D3D11_DEPTH_STENCIL_VIEW_DESC dsvDesc;
				r_tm_memoryZero ( &dsvDesc );
				dsvDesc.Format					= convertImageFormat ( depthDesc.format );
				dsvDesc.ViewDimension			= D3D11_DSV_DIMENSION_TEXTURE2DMS;
				dsvDesc.Texture2D.MipSlice		= 0;
				HRESULT	hr	= device->CreateDepthStencilView ( reinterpret_cast<Texture2DDX11*> ( depthtex )->texture, &dsvDesc, &depthStencil );
				if ( FAILED ( hr ) ) {
					ErrUp();
					RDEBUG_WARRN ( _RST ( "failed create depthstencil %08x\n" ), hr );
					delete	depthtex;
					return	NULL;
				}
				return new DepthStencilDX11 ( depthStencil, depthtex );
			}

			r::draw::Sampler*	CoreDX11 :: createSamplerState ( const SamplerDesc& desc )
			{
				D3D11_SAMPLER_DESC			samplDesc;
				samplDesc.Filter			= convertFilter ( desc.filter );
				samplDesc.AddressU			= convertAddressMode ( desc.addressU );
				samplDesc.AddressV			= convertAddressMode ( desc.addressV );
				samplDesc.AddressW			= convertAddressMode ( desc.addressW );
				samplDesc.MipLODBias		= desc.mipLODBias;
				samplDesc.MaxAnisotropy		= desc.maxAnisotropy;
				samplDesc.ComparisonFunc	= convertComparisonFunc ( desc.comparisonFunc );
				samplDesc.BorderColor[0]	= desc.borderColor.r;
				samplDesc.BorderColor[1]	= desc.borderColor.g;
				samplDesc.BorderColor[2]	= desc.borderColor.b;
				samplDesc.BorderColor[3]	= desc.borderColor.a;
				samplDesc.MinLOD			= desc.minLOD;
				samplDesc.MaxLOD			= desc.maxLOD;
				ID3D11SamplerState*			sampler;
				HRESULT	hr	= device->CreateSamplerState ( &samplDesc, &sampler );
				if ( FAILED ( hr ) ) {
					ErrUp();
					RDEBUG_WARRN ( _RST ( "failed create samplerstate %08x\n" ), hr );
					return	NULL;
				}
				return new SamplerDX11 ( sampler );
			}

			r::draw::RasterizerState*	CoreDX11 :: createRasterizerState ( const RasterizerDesc& desc )
			{
				D3D11_RASTERIZER_DESC				rasterDesc;
				rasterDesc.FillMode					= convertFillMode ( desc.fillMode );
				rasterDesc.CullMode					= convertCullMode ( desc.cullMode );
				rasterDesc.FrontCounterClockwise	= desc.frontCounterClockwise;
				rasterDesc.DepthBias				= desc.depthBias;
				rasterDesc.DepthBiasClamp			= desc.depthBiasClamp;
				rasterDesc.SlopeScaledDepthBias		= desc.slopeScaledDepthBias;
				rasterDesc.DepthClipEnable			= desc.depthClipEnable;
				rasterDesc.ScissorEnable			= desc.scissorEnable;
				rasterDesc.MultisampleEnable		= desc.multisampleEnable;
				rasterDesc.AntialiasedLineEnable	= desc.antialiasedLineEnable;

				ID3D11RasterizerState*		rasterizerState;
				HRESULT	hr	= device->CreateRasterizerState ( &rasterDesc, &rasterizerState );
				if ( FAILED ( hr ) ) {
					ErrUp();
					RDEBUG_WARRN ( _RST ( "failed create rasterizerstate %08x\n" ), hr );
					return	NULL;
				}
				return new RasterizerStateDX11 ( rasterizerState );
			}

			r::draw::BlendState*	CoreDX11 :: createBlendState ( const BlendStateDesc& desc )
			{
				D3D11_BLEND_DESC					blendDesc;
				blendDesc.AlphaToCoverageEnable		= desc.alphaToCoverageEnable;
				blendDesc.IndependentBlendEnable	= desc.independentBlendEnable;
				for ( r_ui32 i = 0; i < 8; ++i ) {
					D3D11_RENDER_TARGET_BLEND_DESC&	targetDesc	= blendDesc.RenderTarget[i];
					const BlendDesc&			blenddesc	= desc.renderTarget[i];
					targetDesc.BlendEnable			= blenddesc.blendEnable;
					targetDesc.SrcBlend				= convertBlend ( blenddesc.srcBlend );
					targetDesc.DestBlend			= convertBlend ( blenddesc.destBlend );
					targetDesc.BlendOp				= convertBlendOP ( blenddesc.blendOp );
					targetDesc.SrcBlendAlpha		= convertBlend ( blenddesc.srcBlendAlpha );
					targetDesc.DestBlendAlpha		= convertBlend ( blenddesc.destBlendAlpha );
					targetDesc.BlendOpAlpha			= convertBlendOP ( blenddesc.blendOpAlpha );
					targetDesc.RenderTargetWriteMask	= convertBlendMask ( blenddesc.renderTargetWriteMask );
				}

				ID3D11BlendState*			blendState;
				HRESULT	hr	= device->CreateBlendState ( &blendDesc, &blendState );
				if ( FAILED ( hr ) ) {
					ErrUp();
					RDEBUG_WARRN ( _RST ( "failed create blendstate %08x\n" ), hr );
					return	NULL;
				}
				return new BlendStateDX11 ( blendState );
			}

			r::draw::DepthStencilState*	CoreDX11 :: createDepthStencilState ( const DepthStencilStateDesc& desc )
			{
				D3D11_DEPTH_STENCIL_DESC		dsDesc;
				dsDesc.DepthEnable				= desc.depthEnable;
				dsDesc.DepthWriteMask			= convertDepthWriteMask ( desc.depthWriteMask );
				dsDesc.DepthFunc				= convertDepthComparison ( desc.depthFunc );
				dsDesc.StencilEnable			= desc.stencilEnable;
				dsDesc.StencilReadMask			= desc.stencilReadMask;
				dsDesc.StencilWriteMask			= desc.stencilWriteMask;
				D3D11_DEPTH_STENCILOP_DESC&		frontdesc	= dsDesc.FrontFace;
				const DepthStencilOPDesc&	frontop	= desc.frontFace;
				frontdesc.StencilFailOp			= convertStencilOp ( frontop.stencilFailOp );
				frontdesc.StencilDepthFailOp	= convertStencilOp ( frontop.stencilDepthFailOp );
				frontdesc.StencilPassOp			= convertStencilOp ( frontop.stencilPassOp );
				frontdesc.StencilFunc			= convertDepthComparison ( frontop.stencilFunc );
				D3D11_DEPTH_STENCILOP_DESC&		backdesc	= dsDesc.BackFace;
				const DepthStencilOPDesc&	backop	= desc.backFace;
				backdesc.StencilFailOp			= convertStencilOp ( backop.stencilFailOp );
				backdesc.StencilDepthFailOp		= convertStencilOp ( backop.stencilDepthFailOp );
				backdesc.StencilPassOp			= convertStencilOp ( backop.stencilPassOp );
				backdesc.StencilFunc			= convertDepthComparison ( backop.stencilFunc );

				ID3D11DepthStencilState*		depthstencilState;
				HRESULT	hr	= device->CreateDepthStencilState ( &dsDesc, &depthstencilState );
				if ( FAILED ( hr ) ) {
					ErrUp();
					RDEBUG_WARRN ( _RST ( "failed create depthstencilstate %08x\n" ), hr );
					return	NULL;
				}
				return new DepthStencilStateDX11 ( depthstencilState );
			}


			r::draw::InputLayout*	CoreDX11 :: createInputLayout ( const InputLayoutDesc& layoutdesc, const VertexShader* shader )
			{
				return new InputLayoutDX11 ( layoutdesc, shader );
			}

			r::draw::VertexBuffer*	CoreDX11 :: createVertexBuffer ( r_ui32 stride, r_ui32 numVertex )
			{
				return new VertexBufferDX11 ( stride, numVertex );
			}

			r::draw::IndexBuffer*	CoreDX11 :: createIndexBuffer ( r_ui32 numIndex )
			{
				return new IndexBufferDX11 ( numIndex );
			}

			r::draw::ConstantBuffer*	CoreDX11 :: createConstantBuffer ( r_ui32 size )
			{
				return new ConstantBufferDX11 ( size );
			}

			r::draw::VertexShader*	CoreDX11 :: createVertexShader ( void* buffer, r_size buffSize, const r_cstr entryFuncName, const r_cstr fileName )
			{
				return new VertexShaderDX11 ( buffer, buffSize, entryFuncName, fileName );
			}

			r::draw::PixelShader*	CoreDX11 :: createPixelShader ( void* buffer, r_size buffSize, const r_cstr entryFuncName, const r_cstr fileName )
			{
				return new PixelShaderDX11 ( buffer, buffSize, entryFuncName, fileName );
			}

			// 開放処理はdeleteを実行できるように変更
			// これで依存関係の意識を少し和らげられる?

			void	CoreDX11 :: destroyTexture2D ( r::draw::Texture2D* texture )
			{
				// 仮に即破棄
				r_tm_comRelease ( r::rtti::dynamicCast<Texture2DDX11*> ( texture )->getResourceView() );
				r_tm_comRelease ( r::rtti::dynamicCast<Texture2DDX11*> ( texture )->getTexture() );
			}

			void	CoreDX11 :: destroyRenderTarget ( r::draw::RenderTarget* target )
			{
				// 仮に即破棄
				r_tm_comRelease ( r::rtti::dynamicCast<RenderTargetDX11*> ( target )->getRenderTarget() );
				r::rtti::dynamicCast<RenderTargetDX11*> ( target )->getTexture()->destroy();
			}

			void	CoreDX11 :: destroyDepthStencil (r::draw:: DepthStencil* target )
			{
				// 仮に即破棄
				r_tm_comRelease ( r::rtti::dynamicCast<DepthStencilDX11*> ( target )->getDepthStencil() );
				r::rtti::dynamicCast<DepthStencilDX11*> ( target )->getTexture()->destroy();
			}

			void	CoreDX11 :: destroySamplerState ( r::draw::Sampler* sampler )
			{
				// 仮に即破棄
				r_tm_comRelease ( r::rtti::dynamicCast<SamplerDX11*> ( sampler )->getSampler() );
			}

			void	CoreDX11 :: destroyRasterizerState ( r::draw::RasterizerState* raster )
			{
				// 仮に即破棄
				r_tm_comRelease ( r::rtti::dynamicCast<RasterizerStateDX11*> ( raster )->getRasterizerState() );
			}

			void	CoreDX11 :: destroyBlendState ( r::draw::BlendState* blend )
			{
				// 仮に即破棄
				r_tm_comRelease ( r::rtti::dynamicCast<BlendStateDX11*> ( blend )->getBlendState() );
			}

			void	CoreDX11 :: destroyDepthStencilState ( r::draw::DepthStencilState* depth )
			{
				// 仮に即破棄
				r_tm_comRelease ( r::rtti::dynamicCast<DepthStencilStateDX11*> ( depth )->getDepthStencilState() );
			}


			void	CoreDX11 :: destroyInputLayout ( r::draw::InputLayout* layout )
			{
				// 仮に即破棄
				r_tm_comRelease( r::rtti::dynamicCast<InputLayoutDX11*> ( layout )->getLayout() );
			}

			void	CoreDX11 :: destroyVertexBuffer ( r::draw::VertexBuffer* buffer )
			{
				// 仮に即破棄
				r_tm_comRelease ( r::rtti::dynamicCast<VertexBufferDX11*> ( buffer )->getVertexBuffer() );
			}

			void	CoreDX11 :: destroyIndexBuffer ( r::draw::IndexBuffer* buffer )
			{
				// 仮に即破棄
				r_tm_comRelease ( r::rtti::dynamicCast<IndexBufferDX11*> ( buffer )->getIndexBuffer() );
			}

			void	CoreDX11 :: destroyConstantBuffer ( r::draw::ConstantBuffer* buffer )
			{
				// 仮に即破棄
				r_tm_comRelease ( r::rtti::dynamicCast<ConstantBufferDX11*> ( buffer )->getConstantBuffer() );
			}

			void	CoreDX11 :: destroyVertexShader ( r::draw::VertexShader* shader )
			{
				// 仮に即破棄
				r_tm_comRelease ( r::rtti::dynamicCast<VertexShaderDX11*> ( shader )->getShader() );
				r_tm_comRelease ( r::rtti::dynamicCast<VertexShaderDX11*> ( shader )->getShaderBuffer() );
			}

			void	CoreDX11 :: destroyPixelShader ( r::draw::PixelShader* shader )
			{
				// 仮に即破棄
				r_tm_comRelease ( r::rtti::dynamicCast<PixelShaderDX11*> ( shader )->getShader() );
			}


			r::draw::RenderTarget*	CoreDX11 :: getRenderTargetCore() const
			{
				return coreRenderTarget;
			}

			r::draw::DepthStencil*	CoreDX11 :: getDepthStencilCore() const
			{
				return coreDepthStencil;
			}

			r::draw::Context*	CoreDX11 :: getContext() const
			{
				// 仮にシングルスレッド
				return threadContext;
			}

			ID3D11ShaderResourceView*	CoreDX11 :: createShaderResourceView ( ID3D11Texture2D* texture ) const
			{
				ID3D11ShaderResourceView			*resource = NULL;
				D3D11_TEXTURE2D_DESC				desc;
				texture->GetDesc ( &desc );
				if ( desc.BindFlags & D3D11_BIND_SHADER_RESOURCE ) {
					D3D11_SHADER_RESOURCE_VIEW_DESC		srvDesc;
					r_tm_memoryZero ( srvDesc );
					srvDesc.Format						= desc.Format;
					srvDesc.ViewDimension				= D3D11_SRV_DIMENSION_TEXTURE2D;
					srvDesc.Texture2D.MostDetailedMip	= 0;
					srvDesc.Texture2D.MipLevels			= desc.MipLevels;

					HRESULT hr = device->CreateShaderResourceView ( texture, &srvDesc, &resource );
					if ( FAILED ( hr ) ) {
						RDEBUG_WARRN ( _RST ( "failed create shader resource view %08x\n" ), hr );
					}
				}
				return resource;
			}

			r_si32 CoreDX11 :: checkErr() const
			{
				return errCount;
			}

			void CoreDX11 :: mirgeCommand()
			{
			}

			void CoreDX11 :: executeCommand()
			{
			}

			void CoreDX11 :: ErrUp() const
			{
				_ErrUp ( errCount );
			}

			void CoreDX11 :: _ErrUp ( const r_si32& errcnt ) const
			{
				const_cast<r_si32&> ( errcnt ) += 1;
			}
		}
	}
}

#endif // R_TARGET_UNIQUEIS(R_TARGET_UNIQUE_WINDX11)
